package mobi.hami.huapin.controller;

/**
 * 管理后台-纸条投放排期相关
 *  * Create by 子龙 on 2018/9/15.
 */

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import mobi.hami.huapin.base.Result;
import mobi.hami.huapin.model.NoteInfo;
import mobi.hami.huapin.model.NoteSchedule;
import mobi.hami.huapin.model.UserReturnGoodsLog;
import mobi.hami.huapin.service.ManagementLaunchService;
import mobi.hami.huapin.util.Dates;
import mobi.hami.huapin.util.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/mangmentlaunch", produces = "application/json")
@Api(description = "供管理后台-纸条投放排期相关api")
public class ManagementLaunchController {

    private static Integer startPageNum = 1;
    private static Integer pageSize = 10;

    @Autowired
    private ManagementLaunchService managementLaunchService;

    /**
     * 某个纸条排期-上线
     *
     * @return
     */
    @RequestMapping(value = "/onlinenoteschedule.do", method = RequestMethod.POST)
    @ApiOperation(value = "某个纸条排期上线", notes = "上线则该坑内该纸条设为上线,该坑内其他已上线纸条设为下线")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{\n" +
                    "  \"success\": true或者false,\n" +
                    "  \"status\": 0成功1失败,\n" +
                    "  \"msg\": \"提示消息\"\n" +
                    "}")
    })
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "主键", example = "1", paramType = "query", required = true, dataType = "int"),
                    @ApiImplicitParam(name = "order_num", value = "坑号", example = "1", paramType = "query", required = true, dataType = "int"),
            }
    )
    public Result onlineNoteSchedule(Integer id, Integer order_num) {
        return managementLaunchService.onlineNoteSchedule(id, order_num);
    }


    /**
     * 某个纸条排期-下线
     *
     * @return
     */
    @RequestMapping(value = "/offlinenoteschedule.do", method = RequestMethod.POST)
    @ApiOperation(value = "某个纸条排期下线", notes = "")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{\n" +
                    "  \"success\": true或者false,\n" +
                    "  \"status\": 0成功1失败,\n" +
                    "  \"msg\": \"提示消息\"\n" +
                    "}")
    })
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "主键", example = "1", paramType = "query", required = true, dataType = "int"),
            }
    )
    public Result offlineNoteSchedule(Integer id) {
        return managementLaunchService.offlineNoteSchedule(id);
    }


    /**
     * 某个纸条排期-修改时间
     *
     * @return
     */
    @RequestMapping(value = "/updatetime.do", method = RequestMethod.POST)
    @ApiOperation(value = "某个纸条排期-修改时间", notes = "")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{\n" +
                    "  \"success\": true或者false,\n" +
                    "  \"status\": 0成功1失败,\n" +
                    "  \"msg\": \"提示消息\"\n" +
                    "}")
    })
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "主键", example = "1", paramType = "query", required = true, dataType = "int"),
                    @ApiImplicitParam(name = "start_time", value = "时间,注意格式为 yyyy-MM-dd HH:mm:ss", example = "1", paramType = "query", required = true, dataType = "String"),
            }
    )
    public Result updateTime(Integer id, String start_time) {
        return managementLaunchService.updateTime(id, Dates.parseDate(start_time));
    }


    /**
     * 某个纸条排期-删除
     *
     * @return
     */
    @RequestMapping(value = "/delete.do", method = RequestMethod.POST)
    @ApiOperation(value = "某个纸条排期-删除", notes = "")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{\n" +
                    "  \"success\": true或者false,\n" +
                    "  \"status\": 0成功1失败,\n" +
                    "  \"msg\": \"提示消息\"\n" +
                    "}")
    })
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "主键", example = "1", paramType = "query", required = true, dataType = "int"),
            }
    )
    public Result delete(Integer id) {
        return managementLaunchService.delete(id);
    }


    /**
     * 纸条排期-列表
     *
     * @param mm
     * @return
     */
    @RequestMapping(value = "/list.do", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "纸条排期-列表", notes = "{\n" +
            "\"pageSize\":每页容量(可选参数),\n" +
            "\"startPageNum\":页码(可选参数),\n" +
            "\"order_num\":坑号(可选参数),\n" +
            "\"sort_id\":分类id(可选参数)\n" +
            "}")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{\n" +
                    "  \"success\": true,\n" +
                    "  \"data\": {\n" +
                    "    \"startPageNum\": 1,\n" +
                    "    \"totalNum\": 1,\n" +
                    "    \"pageSize\": 10,\n" +
                    "    \"totalPageNum\": 1,\n" +
                    "    \"list\": [\n" +
                    "      {\n" +
                    "        \"id\": 2,\n" +
                    "        \"note_id\": 2,\n" +
                    "        \"sort_id\": 1,\n" +
                    "        \"start_time\": \"2018-09-15 16:00:40\",\n" +
                    "        \"content\": \"皮皮虾\",\n" +
                    "        \"order_num\": 1,\n" +
                    "        \"status\": 1\n" +
                    "      }\n" +
                    "    ]\n" +
                    "  }\n" +
                    "}")
    })
    public Result noteschedules(@RequestBody Map<String, Object> mm) {
        HashMap<String, Object> data = new HashMap<String, Object>();
        if (null != mm && null != mm.get("startPageNum")) {
            startPageNum = new Integer(String.valueOf(mm.get("startPageNum")));
        }
        if (null != mm && null != mm.get("pageSize")) {
            pageSize = new Integer(String.valueOf(mm.get("pageSize")));
        }
        Page page = PageHelper.startPage(startPageNum, pageSize, true).reasonable(true);
        List<NoteSchedule> list = managementLaunchService.selectWitchParam(mm);
        PageInfo<NoteSchedule> pi = new PageInfo<>(list);
        int totalRecord = (int) page.getTotal();
        data.put("totalNum", totalRecord);
        int totalPageNum = (totalRecord + pageSize - 1) / pageSize;
        data.put("totalPageNum", totalPageNum);
        data.put("startPageNum", startPageNum);
        data.put("pageSize", pageSize);
        data.put("list", list);
        return Results.successWithData(data);
    }


    /**
     * 纸条排期-纸条分类列表
     */
    @RequestMapping(value = "/notesorts.do", method = RequestMethod.POST)
    @ApiOperation(value = "纸条排期-纸条分类列表", notes = "")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{\n" +
                    "  \"success\": true,\n" +
                    "  \"data\": [\n" +
                    "    {\n" +
                    "      \"id\": 分类id,\n" +
                    "      \"name\": 分类名称,\n" +
                    "      \"create_time\": 创建时间,\n" +
                    "      \"create_user_id\": 创建者id,\n" +
                    "      \"order_num\": 排序号,\n" +
                    "      \"online_time\": 上线时间,\n" +
                    "      \"status\": 状态, 0表示未用，1表示启用,\n" +
                    "      \"version\": 版本号\n" +
                    "    }\n" +
                    "  ]\n" +
                    "}")
    })
    public Result noteSorts() {
        return managementLaunchService.noteSorts();
    }


    /**
     * 纸条排期-纸条搜索
     */
    @RequestMapping(value = "/searchnotes.do", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "纸条排期-纸条搜索", notes = "{\n" +
            "\"comtent\":\"需要搜索的名称(支持模糊搜索)\",\n" +
            "\"start_create_time\":\"创建时间开始区间\",\n" +
            "\"end_create_time\":\"创建时间结束区间\"\n" +
            "\"pageSize\":每页容量(可选参数),\n" +
            "\"startPageNum\":页码(可选参数),\n" +
            "}")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{\n" +
                    "  \"success\": true,\n" +
                    "  \"data\": {\n" +
                    "    \"startPageNum\": 1,\n" +
                    "    \"totalNum\": 9,\n" +
                    "    \"pageSize\": 1,\n" +
                    "    \"totalPageNum\": 9,\n" +
                    "    \"list\": [\n" +
                    "      {\n" +
                    "        \"id\": 9,\n" +
                    "        \"comtent\": \"纸条名称或者内容\",\n" +
                    "        \"cretate_time\": \"创建时间\",\n" +
                    "        \"creator_id\": 创建者id,\n" +
                    "        \"space_id\": 空间id,\n" +
                    "        \"used_times\": 被使用次数,\n" +
                    "        \"thumbup_times\": 被点赞次数,\n" +
                    "        \"note_class_id\": 分类id,\n" +
                    "        \"status\": 状态（0上线,1下线）\n" +
                    "      }\n" +
                    "    ]\n" +
                    "  }\n" +
                    "}")
    })
    public Result searchNotes(@RequestBody Map<String, Object> mm) {
        HashMap<String, Object> data = new HashMap<String, Object>();
        if (null != mm && null != mm.get("startPageNum")) {
            startPageNum = new Integer(String.valueOf(mm.get("startPageNum")));
        }
        if (null != mm && null != mm.get("pageSize")) {
            pageSize = new Integer(String.valueOf(mm.get("pageSize")));
        }
        Page page = PageHelper.startPage(startPageNum, pageSize, true).reasonable(true);
        List<NoteInfo> list = managementLaunchService.searchNotes(mm);
        PageInfo<NoteInfo> pi = new PageInfo<>(list);
        int totalRecord = (int) page.getTotal();
        data.put("totalNum", totalRecord);
        int totalPageNum = (totalRecord + pageSize - 1) / pageSize;
        data.put("totalPageNum", totalPageNum);
        data.put("startPageNum", startPageNum);
        data.put("pageSize", pageSize);
        data.put("list", list);
        return Results.successWithData(data);
    }


    /**
     * 纸条排期-批量添加纸条(全无时间)
     */
    @RequestMapping(value = "/batchaddnoteschedules.do", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "纸条排期-批量添加纸条(全无时间)", notes = "[\n" +
            "  {\n" +
            "    \"note_id\": 纸条id,\n" +
            "    \"sort_id\": 分类id,\n" +
            "    \"content\": \"纸条内容\"\n" +
            "  },\n" +
            "  {\n" +
            "    \"note_id\": 纸条id,\n" +
            "    \"sort_id\": 分类id,\n" +
            "    \"content\": \"纸条内容\"\n" +
            "  }\n" +
            "]")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{\n" +
                    "  \"success\": true或者false,\n" +
                    "  \"status\": 0成功1失败,\n" +
                    "  \"msg\": \"提示消息\"\n" +
                    "}")
    })
    public Result batchAddNoteschedules(@RequestBody NoteSchedule [] ms){
        return  managementLaunchService.batchAddNoteschedules(ms);
    }


    /**
     * 纸条排期-批量添加纸条(某些有时间)
     */
    @RequestMapping(value = "/addnoteschedules.do", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "纸条排期-批量添加纸条(某些有时间)", notes = "[\n" +
            "  {\n" +
            "    \"note_id\": 纸条id,\n" +
            "    \"sort_id\": 分类id,\n" +
            "    \"content\": \"纸条内容\"\n" +
            "    \"start_time\": \"时间\"\n" +
            "    \"order_num\": \"坑号\"\n" +
            "  },\n" +
            "  {\n" +
            "    \"note_id\": 纸条id,\n" +
            "    \"sort_id\": 分类id,\n" +
            "    \"content\": \"纸条内容\"\n" +
            "    \"start_time\": \"时间\"\n" +
            "    \"order_num\": \"坑号\"\n" +
            "  }\n" +
            "]")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{\n" +
                    "  \"success\": true或者false,\n" +
                    "  \"status\": 0成功1失败,\n" +
                    "  \"msg\": \"提示消息\"\n" +
                    "}")
    })
    public Result hAddNoteschedules(@RequestBody NoteSchedule [] ms){
        return  managementLaunchService.addNoteschedules(ms);
    }


}
